<?php


namespace app\app\controller;


use app\common\controller\Homebase;
use think\Db;
use think\facade\Cache;
use util\Http;

class Cmap extends Homebase
{
    protected function initialize()
    {
        parent::initialize();
        $authorization = $_SERVER['HTTP_AUTHORIZATION'] ?? '';
        if ($authorization != 'huizhirenxin') {
            responseJson(-100, '登录信息有误');
        }
    }

    // getAddressByIp  返回当前地址信息
    public function getAddressByIp()
    {
        $address = getCityByIP();
        msg(0, '查询成功', $address);
    }
    //getAddressByLocation2 通过坐标获取位置信息

    //获取资源分类
    public function getPropertyCate()
    {
        $data = Db::name('cmap_cate')
            ->order('sort desc')
            ->where(['is_del' => 0])
            ->select();
        msg(0, '查询完成', $data);
    }

    //市列表  省
    public function city_list()
    {
        $data = $this->request->param();
        //1 单位省 2单位市 3单位区 $data['area_type']
        $area_type = 1;
        try {
            $where[] = ['is_del', '=', 0];
            //xm_type 项目类型 1竞价项目 2推介项目
            if ($data['xm_type']) {
                $where[] = ['xm_type', '=', $data['xm_type']];
            }
            if (isset($data['zong_value']) && $data['zong_value']) {
                $data['zong_value'] = explode('-', $data['zong_value']);
                $data['price1'] = $data['zong_value'][0];
                $data['price2'] = $data['zong_value'][1];
                if ($data['price1'] == 500 && $data['price2'] == -1) {
                    $where[] = ['zong_value', '>', $data['price1'] * 10000];
                } else if ($data['price1'] == -1 && $data['price2'] == 50) {
                    $where[] = ['zong_value', '<', $data['price2'] * 10000];
                } else {
                    if ($data['price2'] < $data['price1']) {
                        msg(-1, '设置区间不正确', 1);
                    }
                    $where[] = ['zong_value', '<=', $data['price2'] * 10000];
                    $where[] = ['zong_value', '>=', $data['price1'] * 10000];
                }
            }
            if ($data['cate_id']) {
                $where[] = ['cate_id', 'in', $data['cate_id']];
            }
            $data_list = getAddressByLocation($data['latitude'], $data['longitude']);
            $data_list = json_decode($data_list, true);
            $return_data = Db::name('cmap')
                ->where([
                    'province' => $data_list['data']['province']
                ])->where($where)->select();
            $groupedItems = [];
            foreach ($return_data as $item) {
                $groupedItems[$item['city']][] = [$item['latitude'], $item['longitude']];
            }
            $marks = [];
            $j = 0;
            foreach ($groupedItems as $k => $v) {
                $marks[$j]['id'] = $j;
                $marks[$j]['name'] = $k;
                $marks[$j]['num'] = count($v);
                $marks[$j]['distance'] = GetCenterFromDegrees($v);
                ++$j;
            }
            msg(0, '', $marks);
        } catch (\Exception $e) {
            msg(-1, $e->getMessage(), '');
        }

    }

    //区县列表  市
    public function areaList()
    {
        $data = $this->request->param();
        try {
            $where[] = ['is_del', '=', 0];
            if ($data['xm_type']) {
                $where[] = ['xm_type', '=', $data['xm_type']];
            }
            if (isset($data['zong_value']) && $data['zong_value']) {
                $data['zong_value'] = explode('-', $data['zong_value']);
                $data['price1'] = $data['zong_value'][0];
                $data['price2'] = $data['zong_value'][1];
                if ($data['price1'] == 500 && $data['price2'] == -1) {
                    $where[] = ['zong_value', '>', $data['price1'] * 10000];
                } else if ($data['price1'] == -1 && $data['price2'] == 50) {
                    $where[] = ['zong_value', '<', $data['price2'] * 10000];
                } else {
                    if ($data['price2'] < $data['price1']) {
                        msg(-1, '设置区间不正确', 1);
                    }
                    $where[] = ['zong_value', '<=', $data['price2'] * 10000];
                    $where[] = ['zong_value', '>=', $data['price1'] * 10000];
                }
            }

            if ($data['cate_id']) {
                $where[] = ['cate_id', 'in', $data['cate_id']];
            }
            $data_list = getAddressByLocation($data['latitude'], $data['longitude']);
            $data_list = json_decode($data_list, true);
            $return_data = Db::name('cmap')
                ->where([
                    'city' => $data_list['data']['city']
                ])->where($where)->select();
            $groupedItems = [];
            foreach ($return_data as $item) {
                $groupedItems[$item['area']][] = [$item['latitude'], $item['longitude']];
            }
            $marks = [];
            $j = 0;
            foreach ($groupedItems as $k => $v) {
                $marks[$j]['id'] = $j;
                $marks[$j]['name'] = $k;
                $marks[$j]['num'] = count($v);
                $marks[$j]['distance'] = GetCenterFromDegrees($v);
                ++$j;
            }
            msg(0, '', $marks);
        } catch (\Exception $e) {
            msg(-1, $e->getMessage(), '');
        }
    }

    //小区列表 区县单位
    public function communityList()
    {
        $data = $this->request->param();
        try {
            $where[] = ['is_del', '=', 0];
            if ($data['xm_type']) {
                $where[] = ['xm_type', '=', $data['xm_type']];
            }
            if (isset($data['zong_value']) && $data['zong_value']) {
                $data['zong_value'] = explode('-', $data['zong_value']);
                $data['price1'] = $data['zong_value'][0];
                $data['price2'] = $data['zong_value'][1];
                if ($data['price1'] == 500 && $data['price2'] == -1) {
                    $where[] = ['zong_value', '>', $data['price1'] * 10000];
                } else if ($data['price1'] == -1 && $data['price2'] == 50) {
                    $where[] = ['zong_value', '<', $data['price2'] * 10000];
                } else {
                    if ($data['price2'] < $data['price1']) {
                        msg(-1, '设置区间不正确', 1);
                    }
                    $where[] = ['zong_value', '<=', $data['price2'] * 10000];
                    $where[] = ['zong_value', '>=', $data['price1'] * 10000];
                }
            }

            if ($data['cate_id']) {
                $where[] = ['cate_id', 'in', $data['cate_id']];
            }
            $data_list = getAddressByLocation($data['latitude'], $data['longitude']);
            $data_list = json_decode($data_list, true);

            $return_data = Db::name('cmap')
                ->where([
                    'area' => $data_list['data']['district']
                ])->where($where)->select();
            $groupedItems = [];
            foreach ($return_data as $item) {
                $groupedItems[$item['community']][] = [$item['latitude'], $item['longitude']];
            }
            $marks = [];
            $j = 0;
            foreach ($groupedItems as $k => $v) {
                $marks[$j]['id'] = $j;
                $marks[$j]['name'] = $k;
                $marks[$j]['num'] = count($v);
                $marks[$j]['distance'] = GetCenterFromDegrees($v);
                ++$j;
            }
            msg(0, '', $marks);
        } catch (\Exception $e) {
            msg(-1, $e->getMessage(), '');
        }
    }

    //获取项目列表
    public function getMapList()
    {
        $data = $this->request->param();
        try {
            $page = input('page/d', 1);
            $length = input('limit/d', 20);
            //area_type 1市级查询 2区县级别查询 3小区查询
            //map_name 地表名称
            $where[] = ['is_del', '=', 0];
            if ($data['area_type'] == 1) {
                $where[] = ['city', '=', $data['map_name']];
            }
            if ($data['area_type'] == 2) {
                $where[] = ['area', '=', $data['map_name']];
            }
            if ($data['area_type'] == 3) {
                $where[] = ['community', '=', $data['map_name']];
            }
            if ($data['xm_type']) {
                $where[] = ['xm_type', '=', $data['xm_type']];
            }
            if (isset($data['zong_value']) && $data['zong_value']) {
                $data['zong_value'] = explode('-', $data['zong_value']);
                $data['price1'] = $data['zong_value'][0];
                $data['price2'] = $data['zong_value'][1];
                if ($data['price1'] == 500 && $data['price2'] == -1) {
                    $where[] = ['zong_value', '>', $data['price1'] * 10000];
                } else if ($data['price1'] == -1 && $data['price2'] == 50) {
                    $where[] = ['zong_value', '<', $data['price2'] * 10000];
                } else {
                    if ($data['price2'] < $data['price1']) {
                        msg(-1, '设置区间不正确', 1);
                    }
                    $where[] = ['zong_value', '<=', $data['price2'] * 10000];
                    $where[] = ['zong_value', '>=', $data['price1'] * 10000];
                }
            }
            if ($data['cate_id']) {
                $where[] = ['cate_id', 'in', $data['cate_id']];
            }
            $return_data = Db::name('cmap')
                ->where($where)
                ->page($page, $length)
                ->select();
            foreach ($return_data as $k => $v) {
                $return_data[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                if ($return_data[$k]['start_time']) {
                    $return_data[$k]['start_time'] = date('Y-m-d H:i:s', $v['start_time']);
                } else {
                    $return_data[$k]['start_time'] = 0;
                }
            }
            msg(0, '', $return_data, $data['map_name']);
        } catch (\Exception $e) {
            msg(-1, $e->getMessage(), '');
        }
    }

    //通过省获取市
    public function getCityList()
    {
        $data = $this->request->param();
        $province_name = $data['province_name'];
        if (isset($data['is_zhi_city']) && $data['is_zhi_city'] == 1) {
            msg(0, '', '');
        }
        if ($data['province_name'] == '广西') {
            $province_name = '广西壮族自治区';
        } elseif ($data['province_name'] == '内蒙古') {
            $province_name = '内蒙古自治区';
        } elseif ($data['province_name'] == '西藏') {
            $province_name = '西藏自治区';
        } elseif ($data['province_name'] == '宁夏') {
            $province_name = '宁夏回族自治区';
        } elseif ($data['province_name'] == '新疆') {
            $province_name = '新疆维吾尔自治区';
        }
        $info = Db::name('area')->where('name', '=', $province_name)->find();
        if ($info['pid'] != 0) {
            msg(0, '', '');
        }
        $cite_list = Db::name('area')->where('pid', $info['id'])->select();
        foreach ($cite_list as $k => $v) {
            $cite_list[$k]['is_able'] = 1;
        }

//        $fruits = array("apple", "banana", "cherry");
//        $insertIndex = 1; // 要插入的位置
//        $element = "orange"; // 要插入的元素
        $c = array('id' => 0, 'name' => '全部', 'pid' => 0, 'is_agent' => 1, 'is_able' => 1);
        array_unshift($cite_list, $c);

        msg(0, '', $cite_list);
    }

    //通过地区获取坐标
    public function a()
    {
        $data = $this->request->param();
        $address = getLWByAddress($data['address_name']);
        $address = json_decode($address, true);
        if ($address['code'] == 0) {
            msg(0, '查询成功', $address['data']);
        } else {
            msg(-1, '查询失败', '');
        }
    }

    public function getzb()
    {
        $address = input('address');
        $key = config('wechat.geocodeMap_ten');
//        $province_name = $data['province_name'];
//        if ($data['province_name'] == '广西') {
//            $province_name = '广西壮族自治区';
//        } elseif ($data['province_name'] == '内蒙古') {
//            $province_name = '内蒙古自治区';
//        } elseif ($data['province_name'] == '西藏') {
//            $province_name = '西藏自治区';
//        } elseif ($data['province_name'] == '宁夏') {
//            $province_name = '宁夏回族自治区';
//        } elseif ($data['province_name'] == '新疆') {
//            $province_name = '新疆维吾尔自治区';
//        }
        if (empty($address)) {
            $this->error("请输入地址！");
        }
        $url = "https://apis.map.qq.com/ws/geocoder/v1?address=" . $address . "&key=" . $key;
        $res = Http::get($url);
        $resq = string2array($res);
        if ($resq["status"] == 0) {
            $data = $resq["result"]["location"];
            msg(0, "获取坐标成功", $data);
        } else {
            msg(-1, "获取坐标失败！" . $resq["message"] . "请资讯腾讯地图", 1);
        }
    }


}